package com.zzx.test.thread.juc;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @ClassName: MySpinLock
 * @Description: 自定义自旋锁
 * @Author zhouzhixin
 * @Date 2021/11/11
 * @Version 1.0
 */
public class MySpinLock {

    AtomicReference<Thread> atomicReference = new AtomicReference<>();

    public void myLock() {
        Thread thread = Thread.currentThread();
        System.out.println(Thread.currentThread().getName() + " -> myLock");

        while (!atomicReference.compareAndSet(null, thread)) {

        }
    }

    public void myUnLock() {
        Thread thread = Thread.currentThread();
        System.out.println(Thread.currentThread().getName() + " -> myUnLock");

        atomicReference.compareAndSet(thread, null);
    }

}
